package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.cqut.dto.ComPriorityDTO;
import com.cqut.dto.CompetitionManagerDTO;
import com.cqut.dto.ProblemManagerDTO;
import com.cqut.pojo.vo.comPriorityVO;
import com.cqut.pojo.vo.problemInfoVO;
import com.cqut.service.ICompetitionManagerService;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.CompetitionManager;
import com.cqut.util.JSONUtil;
import com.cqut.util.ResponseUtil;
import com.cqut.util.DateUtil;

@RestController
@RequestMapping("/api/competitionManager")
public class CompetitionManagerController {
    @Resource(name="competitionManagerService")
    private ICompetitionManagerService competitionManagerService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
    	@RequestParam("competitionName")
        String competitionName,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        
        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> competitionManagerList = competitionManagerService.getComParamList(competitionName,limitShowDTO);
        long count = competitionManagerService.getComParamListCount(competitionName);

        result.put("rows", competitionManagerList);
        result.put("total", count);
        System.out.println(count);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 参赛用户所看到的通过审核的竞赛
     * @param competitionName
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/passList", method = RequestMethod.GET)
    public ResponseEntity<String> passList(
    	@RequestParam("competitionName")
        String competitionName,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        
        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> competitionManagerList = competitionManagerService.getComParamPassList(competitionName,limitShowDTO);
        long count = competitionManagerService.getComParamPassListCount(competitionName);

        result.put("rows", competitionManagerList);
        result.put("total", count);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResponseEntity<String> detail(
    		@RequestParam("competitionId") String competitionId) {
    	
    	 if(competitionId==""||competitionId==null)
             return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	
    	Map<String, Object> result = new HashMap<String, Object>();
    	
        Map<String, Object> competitionManager = competitionManagerService.getComDetailBycompetitionId1(competitionId);
        Map<String, Object> competitionOnwer = competitionManagerService.getComDetailBycompetitionId2(competitionId);

        result.put("manager",competitionManager);
        result.put("owner",competitionOnwer);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 根据大赛ID获取到大赛基本信息以及主办方的相关信息
     * @param priority
     * @return
     */
    @RequestMapping(value = "/comAndHostDetail", method = RequestMethod.GET)
    public ResponseEntity<String> comAndHostDetail(
    		@RequestParam("competitionId") String competitionId) {
    	
    	 if(competitionId==""||competitionId==null)
             return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object>  result = competitionManagerService.getComRecommendDetailById(competitionId);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    
    
    
    @RequestMapping(value = "shiftUp",method = RequestMethod.POST)
    public ResponseEntity<String> shiftUp(
    	@RequestParam("priority")
    		int priority){
    	 if(priority==1)
    		 return ResponseUtil.getResEntityForPPPWhenFail();
    	 return ResponseUtil.getResEntityForPPP(competitionManagerService.shiftUpByPriority(priority));
    }
    
    @RequestMapping(value = "shiftDown",method = RequestMethod.POST)
    public ResponseEntity<String> shiftDown(
    	@RequestParam("priority")
    		int priority){
    	
    	return ResponseUtil.getResEntityForPPP(competitionManagerService.shiftDownByPriority(priority));
    }
       
    @RequestMapping(value = "shiftTop",method = RequestMethod.POST)
    public ResponseEntity<String> shiftTop(
    	@RequestParam("priority")
    		int priority){
    	return ResponseUtil.getResEntityForPPP(competitionManagerService.shiftTopByPriority(priority));
    }
   
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        CompetitionManagerDTO competitionManagerDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        CompetitionManager competitionManagerModel = competitionManagerDTO.toModel();

        return ResponseUtil.getResEntityForPPP(competitionManagerService.save(competitionManagerModel));
    }

    @RequestMapping(value = "/{competitionId}", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@PathVariable("competitionId") String competitionId,
        @Validated
        CompetitionManagerDTO competitionManagerDTO, BindingResult errors) {
    	
        CompetitionManager competitionManagerModel = competitionManagerDTO.toModel();
        competitionManagerModel.setCompetitionId(competitionId);
        return ResponseUtil.getResEntityForPPP(competitionManagerService.update(competitionManagerModel));
    }
    
    @RequestMapping(value = "/updateState", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@RequestParam("competitionId") String competitionId,
    		@RequestParam("state") String state) {
    	System.out.println(competitionId);
    	System.out.println(state);
    	if(state.equals("3")) {
    		return ResponseUtil.getResEntityForPPP(competitionManagerService.updateStateToProgress(competitionId, state));
    	} else {
    		return ResponseUtil.getResEntityForPPP(competitionManagerService.updateStateToStorage(competitionId, state));
    	}

    }

    @RequestMapping(value = "/{competitionId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("competitionId") String competitionId) {
        return ResponseUtil.getResEntityForDel(competitionManagerService.delete(competitionId));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForDel(competitionManagerService.batchDelete(primaryKeys));
    }
    
    /**
     * 初始化列表(竞赛优先设置)
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/comPriorityList", method = RequestMethod.GET)
    public ResponseEntity<String> noticeList(
    	ComPriorityDTO comPriorityDTO, BindingResult uErrors,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        List<comPriorityVO>  result = competitionManagerService.loadComPriorityList(comPriorityDTO, limitShowDTO);

        return ResponseUtil.getResEntityForGet(JSONUtil.toJSONArrayForJavaObject(result).toJSONString());
    }
    
    /**
     * 根据搜索条件进行筛选列表数量
     */
    @RequestMapping(value="/comPriorityCount", method=RequestMethod.GET)
	public ResponseEntity<String> findCompetitionsWithLimit(
			ComPriorityDTO comPriorityDTO, BindingResult uErrors) {
    	if (uErrors.hasErrors())
			ResponseUtil.getResponseEntityWhenInvalidReqParams();
		JSONObject json = new JSONObject();
		json.put("totalItems", competitionManagerService.findCountOfCompetitions(comPriorityDTO));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
    
    /**
     * 根据大赛ID获取到大赛关注数
     */
    @RequestMapping(value="/followNum", method=RequestMethod.GET)
	public ResponseEntity<String> findFollowNum(
			@RequestParam("competitionId") String competitionId) {
    	
    	if(competitionId==""||competitionId==null)
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
    	JSONObject json = new JSONObject();
		json.put("totalItems", competitionManagerService.findNumOfFollow(competitionId));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
}
